﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebSysCore;
using WebSysCore.Helpers;

namespace HSchSchedule.BackendPage
{
    public partial class RegistryPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadProvince();
            }
        }

        private void LoadProvince()
        {
            List<Province> lst = DataBase.GetAllProvince();
            dropProvince.DataSource = lst;
            dropProvince.DataBind();
        }

        private void Insert()
        {
            Customer enCus = new Customer();
            enCus.BankAccount = txtBankAcount.Text.Trim();
            enCus.BankName = txtBankName.Text.Trim();
            enCus.ContactName = txtContactName.Text.Trim();
            enCus.ContactPhone = txtContactPhone.Text.Trim();
            enCus.CustomerAddress = txtAddress.Text.Trim();
            enCus.CustomerDistrictOrCity = txtDistrict.Text.Trim();
            enCus.CustomerName = txtCustomerName.Text.Trim();
            enCus.CustomerPhone = txtCustomerPhone.Text.Trim();
            enCus.CustomerProvinceId = int.Parse(dropProvince.SelectedValue);
            enCus.Email = txtEmail.Text.Trim();
            enCus.LoginName = txtLoginName.Text.Trim();
            enCus.Password = txtPasswowrd.Text.Trim();
            enCus.Deleted = 0;
            string pass = txtPass1.Text.Trim();
            string email = txtEmail1.Text.Trim();

            string err = "";
            bool ok = true;
            ok = ValidateHelp.ValidateCustomer(enCus, out err);
            if (!ok)
            {
                lblError.Text = err;
            }
            else
            {
                if (string.Compare(enCus.Password, pass, false) != 0)
                {
                    err += "Passwors xác thực không trùng nhau,";
                    ok = false;
                }
                else
                {
                    if (string.Compare(enCus.Email, email, false) != 0)
                    {
                        err += "Email xác thực không trùng nhau.";
                        ok = false;
                    }
                }
            }

            if (!ok)
            {
                lblError.Text = err;
            }
            else
            {
                DataBase.InsertCustomer(enCus);
                Response.Redirect("~/BackendPage/MasterPage.aspx");
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Insert();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~//BackendPage/MasterPage.aspx");
        }
    }
}
